package com.karson.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.model.AlbumInfo;

import com.karson.common.request.AlbumRequest;
import com.karson.common.request.AlbumSearchRequest;
import com.karson.common.request.PageParamRequest;
import com.karson.common.response.AlbumRJCode;
import com.karson.common.response.AlbumResponse;
import com.karson.common.response.CommonResult;

import com.karson.common.vo.AlbumFromVo;
import com.karson.service.dao.AlbumAuthorMapper;
import com.karson.service.dao.AlbumTagsMapper;
import com.karson.service.service.AlbumAuthorService;
import com.karson.service.service.AlbumInfoService;
import com.karson.service.service.AlbumTagsService;
import com.karson.service.service.TagInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/album")
@Tag(name = "专辑管理接口")
public class AlbumInfoController {

    @Autowired
    private AlbumInfoService albumInfoService;
    @Autowired
    private AlbumAuthorMapper albumAuthorMapper;
    @Autowired
    private AlbumTagsMapper albumTagsMapper;
    @Operation(summary = "分页列表")
    @PostMapping("/list")
    public CommonResult<IPage<AlbumResponse>> getList(@RequestBody @Validated @ParameterObject AlbumSearchRequest albumSearchRequest) {
        IPage<AlbumResponse> albumResponseIPage = new Page<>(albumSearchRequest.getCurrent(), albumSearchRequest.getSize());
        IPage<AlbumResponse> userCommonPage =  albumInfoService.pageList(albumResponseIPage,albumSearchRequest);
        return CommonResult.success(userCommonPage);
    }
    @Operation(summary = "专辑新增")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody AlbumRequest albumRequest) {
        albumInfoService.saveAlbumInfo(albumRequest);
        return CommonResult.success();
    }
    @Operation(summary = "根据Id获取专辑")
    @GetMapping("/getAlbumById/{id}")
    public CommonResult<AlbumResponse> getAlbumInfoById(@PathVariable("id") Integer albumId) {
        AlbumResponse albumResponse = albumInfoService.getAlbumInfoById(albumId);
        return albumResponse == null ? CommonResult.failed("获取失败") : CommonResult.success(albumResponse);
    }
    @Operation(summary = "表单回显")
    @GetMapping("/getAlbumFrom/{id}")
    public CommonResult<AlbumFromVo> getAlbumFrom(@PathVariable("id") Integer albumId) {
        AlbumInfo albumInfo = albumInfoService.getById(albumId);
        AlbumFromVo albumFromVo = BeanUtil.copyProperties(albumInfo, AlbumFromVo.class);
        List<Integer> tagIdList = albumTagsMapper.getList(albumId);
        List<Integer> authorIdList = albumAuthorMapper.getList(albumId);
        albumFromVo.setAuthorId(authorIdList);
        albumFromVo.setTags(tagIdList);
        return CommonResult.success(albumFromVo);
    }
    @Operation(summary = "修改专辑元信息")
    @PutMapping("/update")
    public CommonResult<String> update(@RequestBody AlbumRequest albumRequest) {
        if (albumRequest == null) {
            return CommonResult.validateFailed();
        }
        albumInfoService.editAlbumInfo(albumRequest);
        return CommonResult.success();
    }
    @Operation(summary = "删除")
    @DeleteMapping("/del/{id}")
    public CommonResult<String> del(@PathVariable("id") Integer albumId) {
        albumInfoService.removeByAlbumId(albumId);
        return CommonResult.success();
    }
    @Operation(summary = "为专辑添加标签")
    @GetMapping("/addTag/{albumId}/{tagId}")
    public CommonResult<String> addTag(@PathVariable Integer albumId, @PathVariable Integer tagId) {
        if (albumId != null && albumId < 0) {
            return CommonResult.failed("获取失败");
        }
        Boolean flag = albumInfoService.saveTag(albumId,tagId);
        return  flag ? CommonResult.success() : CommonResult.failed();
    }
    @Operation(summary = "为管理端的虚拟选择器提供支持")
    @GetMapping("/getAlbumRJCode")
    public CommonResult<List<AlbumRJCode>> getAlbumRjCode(){
        List<AlbumRJCode> albumRjCodes =  albumInfoService.getAlbumRjCode();
        return CommonResult.success(albumRjCodes);
    }

}
